package com.geostar.constructionland.zuul.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author:Yell
 * @Since:2020-8-4 17:34
 */
@RestController
@RequestMapping("/constructionland/session")
@Slf4j
public class SessionController {
    @GetMapping(path = "/set")
    public void set(HttpSession session,
                    @RequestParam("name") String name,
                    @RequestParam("pass") String pass) {
        session.setAttribute(name, pass);
    }

    @GetMapping(path = "getAll")
    @ResponseBody
    public Map<String, Object> getAll(HttpServletRequest request) throws Exception{
        HttpSession session = request.getSession();
        session.getId();
        Map<String, Object> result = new HashMap<>();
        for (Enumeration<String> enumeration = session.getAttributeNames(); enumeration.hasMoreElements(); ) {
            String key = enumeration.nextElement();
            Object value = session.getAttribute(key);
            log.info("key:{},value:{}",key,value);
            result.put(key, value);
        }
        return result;
    }
}
